import request from '@/router/request'

// 图片信息
const weatherList = {
  Temp: "/meteo/mr/temperatures/{ts}/imgs", // 温度
  Pressure: "/meteo/mr/pressures/{ts}/imgs", // 气压
  Wind: "/meteo/mr/winds/{ts}/imgs", // 海风
  Waves: "/meteo/mr/waves/{ts}/imgs", // 海浪
  WaterTemp: "/meteo/mr/waterTemps/{ts}/imgs" // 水温
}

// 等值线信息
const isolineList = {
  Pressure: "/meteo/mr/pressures/{ts}/isoline", // 气压
  Waves: "/meteo/mr/waves/{ts}/isoline", // 海浪
  WaterTemp: "/meteo/mr/waterTemps/{ts}/isoline" // 水温
}

// 获取天气token
export function getMeteoToken() {
  return request({
    url: "/meteo/us/users/open/token",
    method: "post",
    data: {
      appId: "inuorui8888",
      appKey: "!dp3Xld4DbdtwR"
    }
  })
}

// 获取图片
export function getWeatherImage(token, type, ts = parseInt(+new Date() / 1000)) {
  const url = weatherList[type].replace(/{ts}/, ts)
  return request({
    url: url,
    method: "get",
    headers: {
      token: token,
      isToken: false
    }
  })
}

// 获取等值线信息
export function getIsolineImage(token, type, ts = parseInt(+new Date() / 1000)) {
  if (!isolineList[type]) return new Promise((r) => { r(false) })
  const url = isolineList[type].replace(/{ts}/, ts)
  return request({
    url: url,
    method: "get",
    headers: {
      token: token,
      isToken: false
    }
  })
}

// 台风
export function getTyphoonList(token) {
  return request({
    url: "/meteo/typhoon/v2/forecast/newest",
    method: "get",
    headers: {
      token: token,
      isToken: false
    }
  })
}

// 获取风速风向JSON
export function getWindsJson(token, ts = parseInt(+new Date() / 1000)) {
  return request({
    url: `/meteo/mr/winds/${ts}/json`,
    method: "get",
    headers: {
      token: token,
      isToken: false
    }
  })
}

// 单点气象
export function getMeteoByLonLat(token, data) {
  return request({
    // url: "/meteo/mr/netcdf/meteo/comprehensive",
    url: "/meteo/mr/netcdf/meteo/ytx",
    method: "post",
    headers: {
      token: token,
      isToken: false
    },
    data
  })
}
